<template>
  <div class="bottom-bar">
       <div class="check-con" @click="cbAll">
           <check-button  class="check-btn" :is-checked="isSelectAll"></check-button>
           <span>全选</span>
       </div>
       <div class="total-price">
          合集：{{totalPrice}}
       </div>
       <div class="buy" @click="buyClick">
           去结算（{{buy}}）
       </div>
  </div>
</template>
<script>
//公共组件
import CheckButton from 'components/content/checkButton/CheckButton'
export default {
  name: 'CodeCartbuttonbar',
  data(){
    return {
    }
  },
  components: {
    CheckButton
   },
  directives: {  },
  computed: {
   totalPrice(){
     return`￥`+ this.$store.state.cartList.filter(item=>{
         return item.checked
     }).reduce((preValue,item)=>{
       return preValue+item.realPrice * item.count
     },0).toFixed(2)
   },
  buy(){
    return this.$store.state.cartList.filter(item =>item.checked).length
   },
   isSelectAll(){
    //return !(this.$store.state.cartList.filter(item =>!item.checked).length)
    // for(let i=0;i<this.$store.state.cartList.length;i++){
    //     //console.log(this.$store.state.cartList[i].checked)
    // }
    // if(this.$store.state.cartList.length ===0 ) return false
    // return this.$store.state.cartList.find(item=>!item.checked)
     if(this.$store.state.cartList.length === 0 ) return false
      for(let item of this.$store.state.cartList){
           if(!item.checked){
             return false
           }
       }
       return true;
   }
  },
  data() {
    return {
      
    };
  },
  mounted() {
    
  },
  methods: {
    cbAll(){
      //this.isSelectAll 表示全部选中
       if(this.isSelectAll){
          this.$store.state.cartList.forEach(item=>item.checked=false)
       }else{
          this.$store.state.cartList.forEach(item=>item.checked=true)
       }
    },
    buyClick(){
       if(this.$store.state.cartList.length === 0 ){
          this.$toast('请添加商品~')
       }
    }
  },
};
</script>
<style  scoped>
  .bottom-bar{
    width: 100%;
    height: 40px;
    position: relative;
    background: rgb(249, 250, 250);
    display: flex;
    justify-content: left;
    flex-flow: row nowrap;
    border-top:1px solid #eee;
  }
  .check-con{
    margin-left: 10px;
    width: 100px;
    display: flex;
    height: 40px;
  }
 .check-con span{
    display: block;
    font-size: 16px;
    line-height: 40px;
    margin-left: 10px;
 }
  .check-btn{
    width: 30px;
    height: 30px;
    align-self: center;
  }
  .total-price{
    /* width: 120px; */
    line-height: 40px;
    font-size: 16px;
    text-align: justify;
    margin-right: 100px;
  }
  .buy{
    width: 100px;
    height:40px;
    font-size: 12px;
    color:#fff;
    background: rgb(160, 70, 70);
    text-align: center;
    line-height: 40px;
    position:absolute;
    right:0;
    top:0;
  }
</style>